package com.mallbook.controller.servlet.admin;

import com.mallbook.controller.servlet.front.UserServlet;
import com.mallbook.model.exception.AccountBanException;
import com.mallbook.model.exception.AccountNotExitsException;
import com.mallbook.model.exception.AccountPwdErrorException;
import com.mallbook.model.exception.ServiceException;
import com.mallbook.model.pojo.Admin;
import com.mallbook.model.pojo.User;
import com.mallbook.model.service.AddressService;
import com.mallbook.model.service.AdminService;
import com.mallbook.model.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-07-08
 */
@WebServlet("/adminLogin")
public class AdminServlet extends HttpServlet {
    private AdminService adminService = new AdminService();
    private static final Logger logger = LoggerFactory.getLogger(UserServlet.class);


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String opr = req.getParameter("opr");
        if (opr == null) {
            req.getRequestDispatcher("/WEB-INF/admin/login.jsp").forward(req, resp);
        } else if (opr.equals("goLogin")) {
            doGoLogin(req, resp);
        } else if (opr.equals("loginOut")) {
            doLoginOut(req, resp);
        }
    }

    private void doLoginOut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        LocalDateTime date = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String lastTime = formatter.format(date);
        req.getRemoteAddr();// 获取IP地址
        Admin admin = (Admin) session.getAttribute("ADMIN");
        try {
            adminService.updateUser(null, null, lastTime, null, req.getRemoteAddr(), admin.getId());
        } catch (ServiceException e) {
            e.printStackTrace();
            logger.error("管理员登录日志记录失败");
        }
        session.removeAttribute("ADMIN");
        resp.sendRedirect("/admin");
    }

    private void doGoLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String captcha = req.getParameter("captcha");
        if (req.getSession().getAttribute("captcha") == null) {
            resp.sendRedirect("captcha");
            return;
        }
        String captchaSession = (String) req.getSession().getAttribute("captcha");
        if (!captcha.equalsIgnoreCase(captchaSession)) {
            req.setAttribute("msg", "验证码错误");
            req.getRequestDispatcher("/WEB-INF/admin/login.jsp").forward(req, resp);
            return;
        }

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        HttpSession session = req.getSession();
        if (username.trim().isEmpty()) {
            req.setAttribute("msg", "账号无效");
            req.getRequestDispatcher("/WEB-INF/admin/login.jsp").forward(req, resp);
            return;
        }
        if (password.trim().isEmpty()) {
            req.setAttribute("msg", "密码无效");
            req.getRequestDispatcher("/WEB-INF/admin/login.jsp").forward(req, resp);
            return;
        }
        try {
            Admin u = adminService.login(username, password);

            session.setAttribute("ADMIN", u);
            resp.sendRedirect("/admin");
        } catch (AccountNotExitsException e) {
            e.printStackTrace();
            req.setAttribute("msg", "账号不存在");
            req.getRequestDispatcher("/WEB-INF/admin/login.jsp").forward(req, resp);
        } catch (AccountPwdErrorException e) {
            e.printStackTrace();
            req.setAttribute("msg", "密码错误");
            req.getRequestDispatcher("/WEB-INF/admin/login.jsp").forward(req, resp);
        } catch (AccountBanException e) {
            e.printStackTrace();
            req.setAttribute("msg", "您的账号被禁止登录");
            req.getRequestDispatcher("/WEB-INF/admin/login.jsp").forward(req, resp);
        } catch (ServiceException e) {
            e.printStackTrace();
            req.setAttribute("msg", "系统维护中");
            req.getRequestDispatcher("/WEB-INF/admin/login.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
